<?php
namespace common\services;

use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\ExhibitionMeeting;
use common\models\UserMeeting;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class UserMeetingServices extends UserMeeting
{

    /**
     * 添加用户提醒记录
     * @param $userId
     * @param $exhibition_id
     * @param $meeting_id
     * @return array
     */
    public function userInsertMeeting($userId, $exhibition_id, $meeting_id){
        $model = $this->hasUserMeetingData($userId, $meeting_id);
        if (!empty($model)) {
            return ['code' => -3, 'data' => [], 'msg' => '已经登记过'];
        }
        $meeting = ExhibitionMeeting::findOne($meeting_id);
        if (empty($meeting)) {
            return ['code' => -4, 'data' => [], 'msg' => '数据不存在'];
        }
        $model = new UserMeeting();
        $model->exhibition_id = $exhibition_id;
        $model->meeting_id = $meeting_id;
        $model->user_id = $userId;
        $model->starttime = strtotime($meeting->starttime);
        if ($model->save()){
            return ['code' => 0, 'data' => [], 'msg' => 'ok'];
        }else{
            return ['code' => -3, 'data' => [], 'msg' => '登记失败'];
        }
    }

    /**
     * 判断用户是否登记过会议提醒
     * @param $userId
     * @param $meeting_id
     * @return bool|mixed|null|static
     */
    public function hasUserMeetingData($userId, $meeting_id){
        $model = UserMeeting::findOne(['user_id' => $userId, 'meeting_id' => $meeting_id]);
        return !empty($model) ? true : false;
    }

}
